package org.example.everyday;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * Description:https://leetcode-cn.com/problems/lucky-numbers-in-a-matrix/
 * <p>
 * Copyright: 天津联想协同科技有限公司
 * <p>
 *
 * @author anlu
 * @date 2022/2/15 15:04
 */
public class Lc1380 {

    public static void main(String[] args) {
        Lc1380 lc1380 = new Lc1380();
        int[][] nums = new int[][]{{3,7,8},{9,11,13},{15,16,17}};
        List<Integer> list = lc1380.luckyNumbers(nums);
        for (Integer i : list){
            System.out.println(i);
        }
    }

    /**
     * 幸运数字
     * @param matrix
     * @return
     */
    public List<Integer> luckyNumbers (int[][] matrix) {
        List<Integer> list = new ArrayList<>();
        int n = matrix.length, m = matrix[0].length;
        int[] row = new int[n];
        int[] col = new int[m];
        for (int i = 0; i < n; i++) {
            row[i] = Integer.MAX_VALUE;
            for (int j = 0; j < m; j++) {
                row[i] = Math.min(row[i], matrix[i][j]);
                col[j] = Math.max(col[j], matrix[i][j]);
            }
        }

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                int temp = matrix[i][j];
                if(temp == row[i] && temp == col[j]){
                    list.add(temp);
                }
            }
        }
        return list;
    }

}
